<?php
namespace app\index\controller;
use app\model\Session as SessionModel;
use think\Db;


class Session extends Base
{
    public function __construct(){
        $model = new SessionModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;
        $companys = Db::name('company')->select();
        $companys = arrayParamExtract($companys,'id','name');

        $fields = [
            'company_id'=>['name'=>'所属公司','type'=>'select2_1','params'=>$companys],
            'name'=>['name'=>'部门名称','type'=>'text','valids'=>['required']],
            'code'=>['name'=>'部门代码','type'=>'text'],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $companys = Db::name('company')->select();
        $companys = arrayParamExtract($companys,'id','name');

        $search_params = [
            [
                ['key'=>'company_id','name'=>'所属公司','type'=>'select1_1','params'=>$companys],
                ['key'=>'name','name'=>'部门名称','type'=>'text'],
                ['key'=>'code','name'=>'部门代码','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $ulogin = $this->login_user;
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
            ['key'=>'code','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['company'] = Db::name('company')->field('name')->where('id',$vo['company_id'])->find();
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

 

    public function save(){
        $assistant_id = input('assistant_id');
        $ulogin = $this->login_user;

        $save_data = [
            'user_id'=>$ulogin['id'],
            'assistant_id'=>$assistant_id,
            'session_sn'=>uuidCreate(),
            'ctime'=>time()
        ];

        SessionModel::insertGetId($save_data);

        $assistant = Db::name('assistant')->where('id',$assistant_id)->find();
        // $msg_data = [
        //     'user_id'=>$ulogin['id'],
        //     'session_sn'=>$save_data['session_sn'],
        //     'message'=>$assistant['description'],
        //     'ctime'=>time(),
        //     'type'=>1
        // ];
        // Db::name('msg')->insert($msg_data);

        
        $result = [
            'code'=>2000,
            'session_sn'=>$save_data['session_sn'],
            'assistant'=>$assistant,
            'msg'=>[
                'message'=>$assistant['description'],
                'ctime'=>date('y-m-d H:i')
            ]
        ];
        return json($result);
    }

    public function delete($session_sn){
        $model = $this->model;
        if ( $model->where('session_sn',$session_sn)->delete() ) {
            return json(['code'=>2000]);
        }else{
            return json(['code'=>5000,'msg'=>'删除失败']);
        }
    }

    public function clear($session_sn){
        if ( Db::name('msg')->where('session_sn',$session_sn)->delete() !== false ) {
            return json(['code'=>2000]);
        }else{
            return json(['code'=>5000,'msg'=>'删除失败']);
        }
    }




   
}
